package com.fhz.day008;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fhz.day008.adapter.WelcomeAdapter;
import com.fhz.day008.fragment.OneFragment;
import com.fhz.day008.fragment.ThreeFragment;
import com.fhz.day008.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class WelcomeActivity extends AppCompatActivity {
    private ViewPager vp;
    private LinearLayout ll;
    private ThreeFragment  threeFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);


        //第一次有欢迎页
        SharedPreferences welcome = getSharedPreferences("welcome", MODE_PRIVATE);
        boolean isStart = welcome.getBoolean("isStart", false);
        if(isStart){
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }


        vp = (ViewPager) findViewById(R.id.vp);
        ll = (LinearLayout) findViewById(R.id.ll);

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new OneFragment());
        fragmentList.add(new TwoFragment());
        threeFragment = new ThreeFragment();
        fragmentList.add(threeFragment);

        WelcomeAdapter welcomeAdapter = new WelcomeAdapter(getSupportFragmentManager(), fragmentList);


        //创建集合该把小圆点

        List<ImageView> list =  new ArrayList<>();

        //动态添加 小圆点,并设置颜色
        for (int i = 0; i < fragmentList.size(); i++) {
            ImageView imageView = new ImageView(this);
            if(i == 0){
                imageView.setImageResource(R.drawable.circle_t);
            }else {
                imageView.setImageResource(R.drawable.circle_f);
            }
            list.add(imageView);
            //动态的给布局添加组件
            ll.addView(imageView);
        }

        vp.setAdapter(welcomeAdapter);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if(position == list.size()-1){
                    //呼叫一个fragment.
                    threeFragment.handler.sendEmptyMessage(110);
                }

                //相当页的小圆点变色,其他页面的小圆点变成非选择色
                for (int i = 0; i < list.size(); i++) {
                    ImageView imageView = list.get(i);
                    if(i == position){
                        imageView.setImageResource(R.drawable.circle_t);
                    }else{
                        imageView.setImageResource(R.drawable.circle_f);
                    }
                    list.set(i,imageView);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
       getSharedPreferences("welcome", MODE_PRIVATE)
               .edit()
               .putBoolean("isStart",true)
               .apply();
    }
}